package com.jinjia.ordersys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinjia.ordersys.domain.Person;
import com.jinjia.ordersys.dto.OrderDto;
import com.jinjia.ordersys.dto.PersonDto;
import com.jinjia.ordersys.dto.QueryOrdersDTO;
import com.jinjia.ordersys.service.OrderService;
import com.jinjia.ordersys.service.PersonService;
import com.jinjia.ordersys.util.ConstantUtil;
import com.jinjia.ordersys.vo.CustomerInfoVO;
import com.jinjia.ordersys.vo.PageQueryVo;
import com.jinjia.ordersys.vo.ResponseVo;
import io.swagger.annotations.*;

import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 人员角色-Controller
 * @Author: fenghouzhi
 * @Date: 2018/11/5
 * @Time: 10:10 AM
 */
@Slf4j
@RestController
@Api(tags = "人员信息操作")
@RequestMapping("/api/person")
public class PersonController extends BaseController {

    @Resource
    private PersonService personService;

    @Resource
    private OrderService orderService;

    @GetMapping("/notbindpersons")
    @ApiOperation(value = "获取未绑定人员信息")
    ResponseVo getNotBindPersons(@RequestParam("searchName") String searchName) {
        log.info("getNotBindPersons param searchName info   ----  {}", searchName);
        List<PersonDto> personDtoList = personService.getNotBindPersons(searchName);
        if (personDtoList != null && personDtoList.size() > 0) {
            return ResponseVo.ok("success", personDtoList);
        } else {
            return ResponseVo.warn("获取未绑定的用户列表为null");
        }
    }

    @PutMapping("/{personId}")
    @ApiOperation(value = "绑定人员信息")
    ResponseVo bindPerson(@PathVariable("personId") Long personId, @ModelAttribute Person person) {
        if (StringUtils.isEmpty(person)) {
            return ResponseVo.warn("人员ID为空");
        }
        person.setBindStatus(1);
        log.info("bindPerson person param -----   {}", person);
        Integer updateFlag = personService.updateOrBindPerson(person);
        if (updateFlag > 0) {
            return ResponseVo.ok();
        } else {
            return ResponseVo.warn("绑定人员失败");
        }
    }

    @GetMapping("/{openId}")
    @ApiOperation(value = "根据openId获取人员详细信息")
    ResponseVo getPersonInfoByOpenId(@PathVariable("openId") String openId) {
        if (StringUtils.isEmpty(openId)) {
            return ResponseVo.warn("openId为空");
        }
        return ResponseVo.ok();
    }

    @GetMapping("/customer/{personId}")
    @ApiOperation(value = "查看业务员客户列表")
    ResponseVo getCustomersByPersonId(@PathVariable Long personId) {
        if (null == personId) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        log.info("getCustomersByPersonId personId info: {}", personId);
        List<CustomerInfoVO> customers = orderService.getCustomersByPersonId(personId);
        if (null != customers && customers.size() > 0) {
            PageInfo<CustomerInfoVO> pageInfo = new PageInfo<>(customers);
            return ResponseVo.ok("success", getDataForPage(pageInfo));
        } else {
            return ResponseVo.ok("success", Collections.emptyList());
        }
    }

    @PostMapping("/order")
    @ApiOperation(value = "查看业务员删除的订单")
    ResponseVo getDeleteOrdersByPersonId(@RequestBody QueryOrdersDTO queryOrdersDTO) {
        log.info("queryOrderVo info:    " + queryOrdersDTO);
        try {
            if (null == queryOrdersDTO.getPageQueryVo() || null == queryOrdersDTO
                    .getPersonId()) {
                return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
            }

            PageHelper.startPage(queryOrdersDTO.getPageQueryVo().getPageNum(),
                    queryOrdersDTO.getPageQueryVo().getPageSize());
            List<OrderDto> orderDtoList = orderService.getDeleleOrderDtos(queryOrdersDTO);
            PageInfo<OrderDto> pageInfo = new PageInfo<>(orderDtoList);
            return ResponseVo.ok("success", getDataForPage(pageInfo));
        } catch (Exception e) {
            log.info(e.getMessage(), e);
            return ResponseVo.warn("获取订单列表异常");
        }
    }

    @PostMapping("/order/stop")
    @ApiOperation(value = "查看暂停订单")
    ResponseVo getStopOrdersByPersonId(@RequestBody QueryOrdersDTO queryOrdersDTO) {
        try {
            if (null == queryOrdersDTO.getPageQueryVo() || null == queryOrdersDTO
                    .getPersonId()) {
                return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
            }

            PageHelper.startPage(queryOrdersDTO.getPageQueryVo().getPageNum(),
                    queryOrdersDTO.getPageQueryVo().getPageSize());
            List<OrderDto> orderDtoList = orderService.getStopOrderDtos(queryOrdersDTO);
            PageInfo<OrderDto> pageInfo = new PageInfo<>(orderDtoList);
            return ResponseVo.ok("success", getDataForPage(pageInfo));
        } catch (Exception e) {
            log.info(e.getMessage(), e);
            return ResponseVo.warn("获取订单列表异常");
        }
    }

    @PostMapping("/salesman")
    @ApiOperation(value = "业务员列表")
    ResponseVo getAllSalesmans(@RequestBody PageQueryVo pageQueryVo) {
        if (null == pageQueryVo) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        PageHelper.startPage(pageQueryVo.getPageNum(), pageQueryVo.getPageSize());
        List<Person> personList = personService.getAllPersonForSaleType();
        if (null != personList && personList.size() > 0) {
            PageInfo<Person> pageInfo = new PageInfo<>(personList);
            return ResponseVo.ok("success", getDataForPage(pageInfo));
        } else {
            return ResponseVo.ok("success", Collections.emptyList());
        }
    }

    @PostMapping("")
    @ApiOperation(value = "新增业务员")
    ResponseVo addSaleman(@RequestBody Person person) {
        if (null == person) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        log.info("addSaleman pseson info:  {}", person);
        int addResult = personService.addPerson(person);
        if (addResult < 1) {
            return ResponseVo.warn("新增业务员操作异常");
        }
        return ResponseVo.ok();
    }

}